package gui.Teacher;

import javax.swing.JPanel;

import javax.swing.JButton;

import javax.swing.*;
import javax.swing.border.TitledBorder;

import control.RemoveExamControl;
import BaseClasses.PracticalExam;
import System.MainControl;
import System.SysMode;
import System.SystemObject;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.Color;
import java.util.List;

public class RemoveExamGui extends JPanel implements ActionListener{
	
	//**Class Variables**//
	private static final long serialVersionUID = -7855354128460112583L;
	public JButton btnBack_1;
	public JButton btnRemExam;
	private final JTextField txtPleaseEnterExam = new JTextField();
	private JTextField textField;
	private RemoveExamControl control;
	private JComboBox comboBox_1;
	private PracticalExam exam;
	List<PracticalExam> examlist;
	private JButton btnLoad ;
	//**constructor**//
	public RemoveExamGui() {
	    super();
	    setBackground(SystemColor.activeCaption);
	    control = new RemoveExamControl();
	    initialize();
}

//**This method initialize Remove Exam GUI**//
private void initialize() {
    txtPleaseEnterExam.setEditable(false);
    txtPleaseEnterExam.setBackground(Color.LIGHT_GRAY);
	
    txtPleaseEnterExam.setFont(new Font("Tahoma", Font.BOLD, 12));
    txtPleaseEnterExam.setText("Please Enter Exam ID:");
    txtPleaseEnterExam.setBounds(66, 62, 150, 20);
    txtPleaseEnterExam.setColumns(10);
    add(txtPleaseEnterExam);
   
	setBorder(new TitledBorder(null, "Remove Exam", TitledBorder.LEADING, TitledBorder.TOP, null, null));
	this.setSize(500, 500);
	this.setLayout(null);
	
	JLabel lblSetExamId = new JLabel(" Exam ID:");
	lblSetExamId.setHorizontalAlignment(SwingConstants.CENTER);
	lblSetExamId.setFont(new Font("Tahoma", Font.PLAIN, 14));
	lblSetExamId.setBounds(66, 102, 105, 14);
	add(lblSetExamId);
	
	btnRemExam = new JButton("Remove Exam");
	btnRemExam.addActionListener(this);
	btnRemExam.setBounds(56, 234, 150, 23);
	btnRemExam.setFont(MainControl.guiFont);
	add(btnRemExam);
	
	btnBack_1 = new JButton("Back");
	btnBack_1.addActionListener(this);
	btnBack_1.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
	btnBack_1.setBounds(295, 234, 89, 23);
	add(btnBack_1);	
	
	textField = new JTextField();
	textField.setFont(new Font("Tahoma", Font.BOLD, 13));
	textField.setEditable(false);
	textField.setBounds(95, 138, 206, 64);
	add(textField);
	textField.setColumns(10);
	
	comboBox_1 = new JComboBox();
	comboBox_1.setBounds(188, 93, 71, 20);
	SystemObject sys = new SystemObject(null, SysMode.getExam);
	control.sendToServer(sys);
	examlist = null;
	SystemObject retObj = control.GetSysObj();
	if (retObj.getMode() == SysMode.OK)
		examlist = (List<PracticalExam>)retObj.getObj();

	if (examlist != null)
	{
		for (int i=0;i<examlist.size();i++)
		{
			comboBox_1.addItem(examlist.get(i).getExamID());
		}
	}
	add(comboBox_1);
	
	btnLoad = new JButton("Load");
	btnLoad.setFont(new Font("Tahoma", Font.BOLD, 13));
	btnLoad.addActionListener(this);
	btnLoad.setBounds(308, 92, 89, 23);
	add(btnLoad);
	
	JScrollBar scrollBar = new JScrollBar();
	scrollBar.setBounds(80, 138, 17, 64);
	add(scrollBar);
	}

@Override			
public void actionPerformed(ActionEvent e) 
{
	if(e.getSource() == btnBack_1)
	{
		MainControl.setGUI(MainControl.currentUser.getRole(), this);
	}
	
	if(e.getSource()==btnRemExam)
	{
		String exmid=(String)comboBox_1.getSelectedItem();
		exam = new PracticalExam(exmid);
		control = new RemoveExamControl();
		SystemObject sys = new SystemObject(exam , SysMode.deleteExam);
			if (control.sendToServer(sys)==true)//retObj.getMode() == SysMode.OK)
			{
			JOptionPane.showMessageDialog(null,"Delete Exam","System Message",JOptionPane.INFORMATION_MESSAGE);
			MainControl.setGUI(MainControl.currentUser.getRole(), this);
			}
	}
	
	if(e.getSource()==btnLoad)
	{
		int Qid=comboBox_1.getSelectedIndex();
		textField.setText(examlist.get(Qid).getCourse());
	}
}
}